package com.erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.Util.WebUtil;
import com.erp.comm.DataAndView;
import com.erp.comm.ResultCode;
import com.erp.pojo.Goods;
import com.erp.pojo.Inport;
import com.erp.pojo.Provider;
import com.erp.pojo.UserInfo;
import com.erp.service.GoodsService;
import com.erp.service.InportService;
import com.erp.service.ProviderService;
import com.erp.vo.InportVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("inport")
public class InportController {

    @Autowired
    private InportService inportService;
    @Autowired
    private ProviderService providerService;
    @Autowired
    private GoodsService goodsService;

    @RequestMapping("loadAllInport")
    public DataAndView loadAllInport(InportVo inportVo){

        System.out.println(inportVo.getStartTime());
        IPage<Inport> page = new Page<Inport>(inportVo.getPage(),inportVo.getLimit());

            QueryWrapper<Inport> qw = new QueryWrapper<>();
            //如果条件不为空，就添加
            if (inportVo.getProviderid()!=null &&inportVo.getProviderid()!=0){
                qw.like("providerid",inportVo.getProviderid());
            }
            if (inportVo.getGoodsid()!=null && inportVo.getGoodsid()!=0){
                qw.like("goodsid",inportVo.getGoodsid());
            }

            //把String 转化为Date
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startTime = null;
        Date endTime = null;

            if(inportVo.getStartTime()!=null && inportVo.getStartTime()!=""){
                try{
                    startTime = sdf.parse(inportVo.getStartTime());
                }catch (Exception e){
                }
                qw.gt("inporttime",new Timestamp(startTime.getTime()));
            }
            if(inportVo.getEndTime()!=null && inportVo.getEndTime()!=""){
                try{
                    endTime = sdf.parse(inportVo.getEndTime());
                }catch (Exception e){

                }
                qw.lt("inporttime",new Timestamp(endTime.getTime()));
            }
            if (inportVo.getOperateperson() !=null){
                qw.like("operateperson",inportVo.getOperateperson());
            }
            if(inportVo.getRemark()!=null){
                qw.like("remark",inportVo.getRemark());
            }


        //查询
        inportService.page(page,qw);
        List<Inport> list = page.getRecords();
        List<Inport>list2 =new ArrayList<>();
        for(Inport inport:list){
            // 获取当前商品的供货商id
            Integer provideId = inport.getProviderid();
            //通过供货商id 查对应的供货商
            Provider provider = providerService.getById(provideId);
            // 设置当前的商品的供货商名称
            inport.setProvidername(provider.getProvidername());

            Integer goodsId=inport.getGoodsid();
            Goods good = goodsService.getById(goodsId);
            //设置商品名称
            inport.setGoodsname(good.getGoodsname());
            //设置商品规格
            inport.setSize(good.getSize());

            list2.add(inport);
        }

        return new DataAndView(list2);
    }

    //http://1ocalhost:8080/inport/addInport
    @RequestMapping("addInport")
    public ResultCode addInport(Inport inport) {

        inport.setId(null);

        //给默认值
        //inport.setGoodsid(inport.getGoodsid());
        //设置操作员信息
        HttpSession session = WebUtil.getSession();
        UserInfo user = (UserInfo) session.getAttribute("user");
        inport.setOperateperson(user.getName());
        inport.setInporttime(new Date());
        boolean bool = inportService.save(inport);
        if (bool) {
            return ResultCode.SUCCESS_ADD;
        } else {
            return ResultCode.FAIL_ADD;
        }
    }
    //ur1  http://1ocalhost:8080/inport/updateInport
    @RequestMapping("updateInport")
    public ResultCode updateInport(Inport inport){
            try{
                boolean bool = inportService.updateById(inport);
            if(bool){
                return ResultCode.SUCCESS_UPDATE;
            }else{
                return ResultCode.FAIL_UPDATE;
            }
            }catch(Exception e){
                return ResultCode.FAIL_ADD;
            }

    }

    // http://1ocalhost:8080/inport/deleteInport
    @RequestMapping("deleteInport")
    public ResultCode deleteInport(Inport id){
        try {
            boolean bool = inportService.removeById(id);
            System.out.println(bool);
            if (bool) {
                return ResultCode.SUCCESS_DELETE;
            } else {
                return ResultCode.FAIL_DELETE;
            }
        }catch (Exception e){
            return ResultCode.FAIL_DELETE;
        }
    }






}